<?php
$loader->requireOnce('ordo/QueueRoom.class.php');

class Room extends ORDataObject{

	var $room_id	    = '';
	var $vi_name		= '';
	var $en_name		= '';
	var $vi_description = '';
	var $en_description = '';
	var $department_id  = '';
	var $enum_room_type = '';

	var $_table 		= 'room';
	var $_internalName	= 'Room';
	var $_key		= 'room_id';

	function Room($id = 0)	{
		//call the parent constructor so we have a _db to work with
		parent::ORDataObject();
	}

	function get_name() {
		return $this->name;
	}
	/**
	 * @author hdviet
	 * update for localize others
	 */

	public function listRooms() {
		
		$roomName	=	L10nHelper::changeField(L10nHelper::getLocale(), "name");
		$roomDescription	=	L10nHelper::changeField(L10nHelper::getLocale(), "description");

		$ds = new Datasource_sql();
		$sql = array (
			'cols'	=> "t.room_id, t.$roomName as name, t.$roomDescription as description",
			'from' 	=> "$this->_table t",
			'orderby'=> "name"
			);
		$ds->setup($this->_db, $sql);
		return $ds->toArray('room_id', 'name','description');
	}

	public function fromService($servId) {
		$field	=	L10nHelper::changeField(L10nHelper::getLocale(), "name");
		$ds = new Datasource_sql();
		$sql = array (
			'cols'	=> "sr.service_room_id, r.$field name",
			'from'	=> "service_room sr
				INNER JOIN room r ON sr.room_id = r.room_id",
			'where'	=> "sr.service_id = $servId",
			'orderby'=> "r.name"
			);
		$ds->setup($this->_db, $sql);
		return $ds->toArray('service_room_id', 'name');
	}

	/**
	 * @author hdviet
	 * update for localize others
	 */

	function addRoom($room){
		$data = array (
			'department_id'  => $room['room_department'],			
		    'enum_room_type' => $room['room_type']
		);
		
	    $locales = L10nHelper::getSupportedLocales();
		$datalo = array();
       	foreach ( $locales as $lo => $la ) {
       		$nameField		=	L10nHelper::changeField($lo,"name");
			$desField		=	L10nHelper::changeField($lo,"description");
		    //save name and description
       		$data[$nameField] = $room['room_name'];
       		$data[$desField] = $room['room_description'];
       	}
       	
       	$this->populateArray($data);
		$this->persist();
		return $this->room_id;
	}

	function getRoom($roomId){
	    $locale = L10nHelper::getLocale();
		$departmentName	=	L10nHelper::changeField($locale, "name");
		$roomName	=	L10nHelper::changeField($locale, "name");
		$roomDescription	=	L10nHelper::changeField($locale, "description");
		$ds = new Datasource_sql();
		$sql = array(
			'cols' 		=> "room_id, dept.department_id, dept.$departmentName as department_name, 
			         room.$roomName room_name, room.$roomDescription as description, 
			         room.enum_room_type as room_type, room.deleted",
			'from'		=> "room 
			         LEFT JOIN department dept ON (dept.department_id = room.department_id)",
			'where'	=> "room_id = '$roomId'"
		);
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) return false;
		else return $result[0];
	}

	function getAllRooms($filterDepartmentId, $toKeyValueArray = false){
		$departmentName	=	L10nHelper::changeField(L10nHelper::getLocale(), "name");
		$roomName	=	L10nHelper::changeField(L10nHelper::getLocale(), "name");
		$roomDescription	=	L10nHelper::changeField(L10nHelper::getLocale(), "description");
		$ds = new Datasource_sql();
		switch ($filterDepartmentId){
			case 'all':
				$sql = array (
					'cols' 		=> "room_id, dept.department_id, dept.$departmentName as department_name, 
					       room.$roomName room_name, room.$roomDescription as description, 
					       room.enum_room_type as room_type, room.deleted",
					'from'		=> "room 
					       LEFT JOIN department dept ON (dept.department_id = room.department_id)",
					'orderby'	=> "dept.department_id, room_name");
				break;
			case 'other':
				$sql = array (
					'cols' 		=> "room_id, dept.department_id, dept.$departmentName as department_name, 
					       room.$roomName room_name, room.$roomDescription as description, 
					       room.enum_room_type as room_type, room.deleted",
					'from'		=> "room 
					       LEFT JOIN department dept ON (dept.department_id = room.department_id)",
					'where'		=> "room.department_id = 0",
					'orderby'	=> "room_name");
				break;
			default:
				$sql = array (
					'cols' 		=> "room_id, dept.department_id, dept.$departmentName as department_name, room.$roomName room_name, room.$roomDescription as description, room.enum_room_type as room_type, room.deleted",
					'from'		=> "room LEFT JOIN department dept ON (dept.department_id = room.department_id)",
					'where'		=> "dept.department_id = '".$filterDepartmentId."'",
					'orderby'	=> "room_name");
				break;
		}

		$ds->setup($this->_db, $sql);

		if ($toKeyValueArray) {
			return $ds->toArray('room_id', 'room_name');
		}
		return $ds->toArray();
	}

	function updateRoom($room){
		$qr = Celini::newORDO('QueueRoom');
		$save = false;
		if ($room['encounter_room'] == 'true') {
			$save = true;
		}
		$qr->update($room['room_id'], QueueRoom::ENCOUNTER, $save);

		$nameField		=	L10nHelper::changeField(L10nHelper::getLocale(),"name");
		$desField		=	L10nHelper::changeField(L10nHelper::getLocale(),"description");
        $data =	array('department_id'  => $room['room_department'],
                      'enum_room_type' => $room['room_type'],
        );
	    $data[$nameField] = $room['room_name'];
		$data[$desField] = $room['room_description'];
		
        $this->setup($room['room_id']);
		$this->populateArray($data);
		$this->persist();
		
	}

	function removeOrUndoRoom($roomId, $action){
		$sql = "";
		if ($action == 'remove') {
		    $sql = "UPDATE room SET deleted = 1 WHERE room_id = '$roomId'";
		} else if ($action == 'undo') {
			$sql = "UPDATE room SET deleted = 0 WHERE room_id = '$roomId'";
		}
		$this->_execute($sql);
	}
} // end of Class

?>
